# 
library(rvest)
# 
# cargamos la url de la pagina de catastro que procede de introducir la dirección en catastro: CALLE/NUMERO
 
 url.catastro <-"https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?via=VERACRUZ&tipoVia=CL&numero=25&kilometro=&bloque=&escalera=&planta=&puerta=&DescProv=MADRID&prov=28&muni=61&DescMuni=GALAPAGAR&TipUR=U&codvia=38&comVia=VERACRUZ%20(CALLE)&pest=urbana&from=OVCBusqueda&nomusu=%20&tipousu=&ZV=NO&ZR=NO&anyoZV=&tematicos=&anyotem=" 
 
tmp <- read_html(url.catastro) #lee la pagina web
tmp <- html_nodes(tmp, "div.panel.panel-default") # coge los nodos con div,panel.panel-default que son las referencia catastrales del cuadro (inspeccion de la web de catastro)
 
tmp1 <- read_html(url.catastro) #lee la pagina web
tmp1 <- html_nodes(tmp, "#ImgFachada0") # imagen de fachada

obtener el cuadro con las superficies por usos

library(rvest)

webshot ( "https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?via=VERACRUZ&tipoVia=CL&numero=22&kilometro=&bloque=&escalera=&planta=&puerta=&DescProv=MADRID&prov=28&muni=61&DescMuni=GALAPAGAR&TipUR=U&codvia=38&comVia=VERACRUZ%20(CALLE)&pest=urbana&from=OVCBusqueda&nomusu=%20&tipousu=&ZV=NO&ZR=NO&anyoZV=&tematicos=&anyotem=")


PUEDES EDITAR ESTE PARRAFO PARA ESCRIBIR TU PROPIAS NOTAS.


AHORA VAMOS HA HACER LO MISMO, PERO DE FORMA AUTOMATICA, PARTIENDO DE LOS DATOS DE LOCALIZACION DEL INMUEBLE.

DATOS A RELLENAR:

library(rvest)
library(stringr)

via <- "CL"
via1 <- "CALLE"
nombrecalle <- "VERACRUZ"
numero <- "22" 
DescProv <- "MADRID" 
pro <- "28"
DesMuni <- "GALAPAGAR" 
Cpodt <- "28260"



ww <- "https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?via="

# nombrecalle<-gsub(" ", "@", nombrecalle)
# comvia<-gsub("@", "%20", nombrecalle)
# comvia<-paste(comvia, "%20", sep="")
# comvia

ee <- nombrecalle
rr <- "&tipoVia="

ss <- via
zz <- "&numero="
xx <- numero
yy <- "&kilometro=&bloque=&escalera=&planta=&puerta="

tt <- "&DescProv="
cc <- DescProv

vv <- "&DescMuni="
bb <- DesMuni

oo <- "&prov="
pp <- pro

nn <- "&TipUR=U"

gg <- "&comVia="
jj <- nombrecalle


lll <- "&pest=urbana&from=OVCBusqueda&nomusu=%20&tipousu=&ZV=NO&ZR=NO&anyoZV=&tematicos=&anyotem="

# (via)&pest=urbana&from=OVCBusqueda&nomusu=%20&tipousu=&ZV=NO&ZR=NO&anyoZV=&tematicos=&anyotem="

webcatastro <- paste(ww, ee, rr, ss, zz, xx, yy, tt, cc, oo, pp, vv, bb, nn, gg, jj, lll, sep="")
webcatastro
[1] "https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?via=VERACRUZ&tipoVia=CL&numero=22&kilometro=&bloque=&escalera=&planta=&puerta=&DescProv=MADRID&prov=28&DescMuni=GALAPAGAR&TipUR=U&comVia=VERACRUZ&pest=urbana&from=OVCBusqueda&nomusu=%20&tipousu=&ZV=NO&ZR=NO&anyoZV=&tematicos=&anyotem="
tmp1 <- read_html(webcatastro) #lee la pagina web
tmp1 <- html_nodes(tmp1, "div.panel.panel-default") # coge los nodos con div,panel.panel-default que son las referencia catastrales del cuadro (inspeccion de la web de catastro)

head(html_text(tmp1), 10)
 [1] "5222210VK1952S0001JB     CL VERACRUZ 22 Es:D Pl:00 Pt:AResidencial  |             93 m2  |    5,19%  | 2000"
 [2] "5222210VK1952S0002KZ     CL VERACRUZ 22 Es:D Pl:00 Pt:BResidencial  |            102 m2  |    5,80%  | 2000"
 [3] "5222210VK1952S0003LX     CL VERACRUZ 22 Es:D Pl:00 Pt:CResidencial  |             80 m2  |    4,70%  | 2000"
 [4] "5222210VK1952S0004BM     CL VERACRUZ 22 Es:D Pl:00 Pt:DResidencial  |             73 m2  |    4,13%  | 2000"
 [5] "5222210VK1952S0007ME     CL VERACRUZ 22 Es:D Pl:01 Pt:AResidencial  |             97 m2  |    5,51%  | 2000"
 [6] "5222210VK1952S0008QR     CL VERACRUZ 22 Es:D Pl:01 Pt:BResidencial  |            100 m2  |    5,75%  | 2000"
 [7] "5222210VK1952S0009WT     CL VERACRUZ 22 Es:D Pl:01 Pt:CResidencial  |             82 m2  |    4,89%  | 2000"
 [8] "5222210VK1952S0010ME     CL VERACRUZ 22 Es:D Pl:01 Pt:DResidencial  |             91 m2  |    5,27%  | 2000"
 [9] "5222210VK1952S0013EY     CL VERACRUZ 22 Es:D Pl:02 Pt:AResidencial  |            122 m2  |    6,69%  | 2000"
[10] "5222210VK1952S0014RU     CL VERACRUZ 22 Es:D Pl:02 Pt:BResidencial  |            130 m2  |    6,68%  | 2000"

PUEDES EDITAR ESTE PARRAFO PARA ESCRIBIR TU PROPIAS NOTAS.


da<-html_text(tmp1)

nchar(da[1]) # cuantos caracteres tiene la primera fila?
[1] 107
head(substr(da, start = 0, stop = 20))    # Para extraer la subcadena desde el primero al caracter 20 es decir la referencia catastral.
[1] "5222210VK1952S0001JB" "5222210VK1952S0002KZ" "5222210VK1952S0003LX"
[4] "5222210VK1952S0004BM" "5222210VK1952S0007ME" "5222210VK1952S0008QR"
refcat <- substr(da[1], start = 0, stop = 14)      # Para extraer la subcadena desde el primero al caracter 14 es decir la referencia catastral del edificio.
refcat
[1] "5222210VK1952S"
knitr::include_url(webcatastro)


ww <- "https://www1.sedecatastro.gob.es/Cartografia/mapa.aspx?"

re <- "refcat="
ree <- refcat

ee <- nombrecalle
rr <- "&tipoVia="
ss <- via
zz <- "&numero="
xx <- numero
yy <- "&kilometro=&bloque=&escalera=&planta=&puerta="

tt <- "&DescProv="
cc <- DescProv

vv <- "&DescMuni="
bb <- DesMuni

oo <- "&prov="
pp <- pro

nn <- "&TipUR=U"

gg <- "&comVia="
jj <- nombrecalle

lll <- "&pest=urbana&from=OVCBusqueda&nomusu=%20&tipousu=&ZV=NO&ZR=NO&anyoZV=&tematicos=&anyotem="

# (via)&pest=urbana&from=OVCBusqueda&nomusu=%20&tipousu=&ZV=NO&ZR=NO&anyoZV=&tematicos=&anyotem="
# 
locacatastro <- paste(ww, re, ree, sep="")
locacatastro
[1] "https://www1.sedecatastro.gob.es/Cartografia/mapa.aspx?refcat=5222210VK1952S"
knitr::include_url(locacatastro)
webshot ( locacatastro, "C:/Users/polo/Desktop/AEVIU/0002/locacatastro.png")

library(spanish)
coo<-geocode_cadastral(ree, parse_files = FALSE)
coo
[1] "-4.00288119736999,40.57471475288"
library(stringr)

dir1<-strsplit(coo, ",")
dir1
[[1]]
[1] "-4.00288119736999" "40.57471475288"   
lng1<-as.numeric(dir1[[1]][[1]])
lat1<-as.numeric(dir1[[1]][[2]])
coord1<-c(lng1, lat1)
coord1
[1] -4.002881 40.574715
# Use with coords
# 
library(CatastRo)
library(spanish)
library(sp)
library(sf)

direcc<-catr_get_code_from_coords(coord1, srs = 4326)
t(direcc)
           [,1]       
munic      "GALAPAGAR"
catr_to    "28"       
catr_munic "061"      
catrcode   "28061"    
cpro       "28"       
cmun       "061"      
inecode    "28061"    
nm         "GALAPAGAR"
cd         "28"       
cmc        "61"       
cp         "28"       
cm         "61"       
require(knitr)
require(XLConnect)

testigospar<-read_excel("C:/Users/polo/Desktop/RegistroTestigos.xlsm", sheet = "parcela", range = ("A1:l6"))

kable((testigospar),
      "html", 
      col.names = c("numero","direccion", "m2p", "VOfertado", "UAdoptado", "RC", "telefono", "municipio", "lat", "long", "fecha", "web"),
      align = c("l","c","c","c","c","c","c","c","c","c","c","l"))%>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
                full_width = F, 
                position = "float_left")%>%
  kable_styling(font_size = 10)%>%
  column_spec(1, bold=T, border_right = T)
numero direccion m2p VOfertado UAdoptado RC telefono municipio lat long fecha web
testigo1 INFANTA CARLOTA 11 1134 158000 125.3968 7682801VK0878S 682773099 VALDEMORILLO 40.53859 -4.092037 2022-05-01 https://www.idealista.com/inmueble/97262303/
testigo 3 ARROYO DE LOS CHOPOS 11 1200 175000 131.2500 1933526VK1813S 682531942 VALDEMORILLO 40.49134 -4.041554 2022-05-01 https://www.idealista.com/inmueble/93551445/
testigo 4 MONASTERIO DE CASTILLA 53 1173 160000 122.7621 3128002VK1832N 669781152 VALDEMORILLO 40.48938 -4.026196 2022-05-01 https://www.idealista.com/inmueble/95133059/
testigo 5 PALACIOS 19 1118 190000 152.9517 3328538VK1832N 606990145 VALDEMORILLO 40.48895 -4.023575 2022-05-01 https://www.idealista.com/inmueble/97064776/
testigo 6 SIERRA DE JAVALAMBRE 10 1014 165000 146.4497 7890407VK0779N 914895228 VALDEMORILLO 40.46282 -4.088542 2022-04-28 https://www.fotocasa.es/es/comprar/terreno/valdemorillo/cerro-alarcon/161675424/d
options(knitr.kable.NA ="")
library(ggplot2)
# install.packages('ggspatial')
library(ggspatial)
library(CatastRo)
library(spanish)

s <- catr_atom_get_parcels("Valdemorillo",
to = "Madrid",
what = "zoning",     #o "parcel"
)

# class(s), # st_crs(s) <- 4326, # st_crs(s)

dat2 <- select(testigospar, latitud, longitud, DIRECCION)

dat2_sf<-st_as_sf(dat2, coords = c("longitud", "latitud"), crs = st_crs(4326))
dat2_sf
Simple feature collection with 5 features and 1 field
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -4.092037 ymin: 40.46282 xmax: -4.023575 ymax: 40.53859
Geodetic CRS:  WGS 84
# A tibble: 5 x 2
  DIRECCION                             geometry
* <chr>                              <POINT [°]>
1 INFANTA CARLOTA 11        (-4.092037 40.53859)
2 ARROYO DE LOS CHOPOS 11   (-4.041553 40.49134)
3 MONASTERIO DE CASTILLA 53 (-4.026196 40.48938)
4 PALACIOS 19               (-4.023575 40.48895)
5 SIERRA DE JAVALAMBRE 10   (-4.088542 40.46282)
# plot(st_geometry(s), axes = TRUE, col = "blue")
# plot(st_geometry(dat2_sf), col = "white", cex = 1, add = TRUE)

ggplot(s) +
# layer_spatial(capap, size = 3, col="red",alpha = 0.5)+
# geom_sf() +
geom_sf(fill = "brown", color = "black") +
geom_sf(data = dat2_sf, color = "white", cex = 2) +
annotation_north_arrow(location='tr') +
labs(title = "Cadastral Zoning", subtitle = "Valdemorillo, Madrid")


library(spanish)
# usando la referencia catastral para localizar el inmueble
# 
rc <- catr_wfs_get_buildings_rc(ree)
# rc <- catr_wfs_get_parcels_neigh_parcel(ree)
library(ggplot2)
library(CatastRo)
ggplot(rc) +
geom_sf() +
geom_sf(fill = "brown", color = "black") +
annotate("point", x = 415122.4, y = 4492033, colour = "white", cex = 3)+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1, size = 11))+
labs(title = "buscando referencia catastral \ndibujamos el centroide")

# sf::st_centroid(rc)

FOTO DE FACHADA, se guarda en el el directorio indicado

ww <- "http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral="

ree <- refcat

fotocatastro <- paste(ww, ree, sep="")
fotocatastro
[1] "http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=5222210VK1952S"
knitr::include_url(fotocatastro)
webshot ( fotocatastro, "C:/Users/polo/Desktop/AEVIU/0002/fotocatastro.png")

todas las RC de un municipio

# obtener la direccion y coordenadas a partir de la referencia catastral
coordenadas_catastro<-catr_ovc_get_cpmrc("9872023VH5797S")
t(coordenadas_catastro)
         [,1]                                             
xcoord   "-3.463284"                                      
ycoord   "38.64014"                                       
refcat   "9872023VH5797S"                                 
address  "CL GLORIA 51 SANTA CRUZ DE MUDELA (CIUDAD REAL)"
pc.pc1   "9872023"                                        
pc.pc2   "VH5797S"                                        
geo.xcen "-3.46328353028033"                              
geo.ycen "38.6401434830887"                               
geo.srs  "EPSG:4326"                                      
ldt      "CL GLORIA 51 SANTA CRUZ DE MUDELA (CIUDAD REAL)"
# obtener la referencia catastral a partir de las coordenadas
catr_ovc_get_rccoor(lat = 38.6196566583596, lon = -3.45624183836806, srs = 4326, verbose = FALSE)
# A tibble: 1 x 8
  refcat         address           pc.pc1 pc.pc2 geo.xcen geo.ycen geo.srs ldt  
  <chr>          <chr>             <chr>  <chr>     <dbl>    <dbl> <chr>   <chr>
1 13077A01800011 DS DISEMINADO  P~ 13077~ 18000~    -3.46     38.6 EPSG:4~ DS D~
# usanddo bbox
building <- catr_wfs_get_buildings_bbox(c(
415203,
4491991,
415803,
4492591
),
srs = 25830
)
library(ggplot2)
ggplot(building) +
geom_sf() +
labs(title = "Busqueda usando bbox")

# podemos obtener las referencias catastrales e incluso las fotos de las fachadas
head(building, 3)
Simple feature collection with 3 features and 24 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 415122.8 ymin: 4492063 xmax: 415253.3 ymax: 4492621
Projected CRS: ETRS89 / UTM zone 30N
                     gml_id beginLifespanVersion conditionOfConstruction
1 ES.SDGC.BU.5222504VK1952S  2007-05-04T00:00:00              functional
2 ES.SDGC.BU.5226701VK1952N  2003-03-05T00:00:00              functional
3 ES.SDGC.BU.5227602VK1952N  2002-05-28T00:00:00              functional
            beginning                 end endLifespanVersion
1 2002-01-01T00:00:00 2002-01-01T00:00:00               <NA>
2 2000-01-01T00:00:00 2000-01-01T00:00:00               <NA>
3 1993-01-01T00:00:00 1993-01-01T00:00:00               <NA>
                                                                             informationSystem
1 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=5222504&rc2=VK1952S
2 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=5226701&rc2=VK1952N
3 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=5227602&rc2=VK1952N
       reference        localId  namespace horizontalGeometryEstimatedAccuracy
1 5222504VK1952S 5222504VK1952S ES.SDGC.BU                                 0.1
2 5226701VK1952N 5226701VK1952N ES.SDGC.BU                                 0.1
3 5227602VK1952N 5227602VK1952N ES.SDGC.BU                                 0.1
  horizontalGeometryEstimatedAccuracy_uom horizontalGeometryReference
1                                       m                   footPrint
2                                       m                   footPrint
3                                       m                   footPrint
  referenceGeometry    currentUse numberOfBuildingUnits numberOfDwellings
1              TRUE 1_residential                    27                16
2              TRUE 1_residential                   123                47
3              TRUE 1_residential                   400               179
  numberOfFloorsAboveGround
1                      <NA>
2                      <NA>
3                      <NA>
                                                                                                                      documentLink
1 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=5222504VK1952S
2 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=5226701VK1952N
3 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=5227602VK1952N
  format sourceStatus officialAreaReference value value_uom
1   jpeg  NotOfficial        grossFloorArea  1789        m2
2   jpeg  NotOfficial        grossFloorArea  7894        m2
3   jpeg  NotOfficial        grossFloorArea 19826        m2
                        geometry
1 MULTIPOLYGON (((415184.3 44...
2 MULTIPOLYGON (((415172.4 44...
3 MULTIPOLYGON (((415136.1 44...
# Using bbox
building1 <- catr_wfs_get_buildings_bbox(c(
376550,
4545424,
376600,
4545474
),
srs = 25830
)
library(ggplot2)
ggplot(building1) +
geom_sf() +
geom_sf_label(aes(label = building1$reference), size = 3) # podemos añadir por ejemplo las referencia catastrales

labs(title = "Busqueda usando bbox")
$title
[1] "Busqueda usando bbox"

attr(,"class")
[1] "labels"
building1$reference
[1] "6656601UL7465N" "6656602UL7465N" "6656603UL7465N" "6656606UL7465N"
[5] "6656607UL7465N" "6756311UL7465N" "6756312UL7465N" "6756314UL7465N"
# utilizamos la funcion plot para dibujar la geometria del objeto building1 y coloreamos el primer elemento
# de color rojo

plot(st_geometry(building1), bg = "azure", graticule = TRUE, col_graticule = "black", main = "grafico con plot de building1", axes = TRUE, las = 1, cex.axis = 0.75) 

# colocamos los ejes, las lineas y un titulo, valores de "las" 0, 1, 2, 3; ademas con cex.axis cambiaos el tamaño # de las etiquetas de los tiks de los ejes

plot(st_geometry(building1)[1], col = 'red', add = TRUE)

en este caso se incluye la referencia catastral, el centroide y la superficie en m2

rc1 <- catr_wfs_get_buildings_rc("6656601UL7465N")

library(ggplot2)
ggplot(rc1) +
geom_sf() +
geom_sf_label(aes(label = rc1$reference), size = 3, nudge_x = -5, nudge_y = -3, seed = 10) +
geom_sf_label(aes(label = rc1$value), size = 3, nudge_x = 0, nudge_y = +3, seed = 10) +
annotate("point", x = 376551.2, y = 4545476, colour = "red", cex = 5)+ 
labs(title = "Search using rc")

# sf::st_centroid(rc1)
rc2 <- catr_wfs_get_buildings_rc("2013101VL1021S")
library(ggplot2)
ggplot(rc2) +
geom_sf() +
geom_sf_label(aes(label = rc2$reference), size = 3, nudge_x = -5, nudge_y = -5, seed = 10) +
labs(title = "Search using rc")

# With a bbox
pict <- catr_wms_get_layer(c(222500, 4019500, 223700, 4020700),
srs = 25830,
what = "parcel",
# style = "ELFCadastre"
)
library(ggplot2)
library(mapSpain)
ggplot() +
layer_spatraster(pict)



tabla de nivel superior que incluye información de todos las direcciones territoriales (excepto País Vasco y Navarra) 7611 MUNICIIOS

write.table(territorial_office, file = "territorial_office.csv", row.names = FALSE) # guarda un archivo excel
head(territorial_office, 10)
# A tibble: 10 x 4
   territorial_office             url                  munic date               
   <chr>                          <chr>                <chr> <dttm>             
 1 Territorial office 02 Albacete http://www.catastro~ 0200~ 2022-03-14 00:00:00
 2 Territorial office 02 Albacete http://www.catastro~ 0200~ 2022-03-14 00:00:00
 3 Territorial office 02 Albacete http://www.catastro~ 0200~ 2022-03-14 00:00:00
 4 Territorial office 02 Albacete http://www.catastro~ 0200~ 2022-03-14 00:00:00
 5 Territorial office 02 Albacete http://www.catastro~ 0200~ 2022-03-14 00:00:00
 6 Territorial office 02 Albacete http://www.catastro~ 0200~ 2022-03-14 00:00:00
 7 Territorial office 02 Albacete http://www.catastro~ 0200~ 2022-03-14 00:00:00
 8 Territorial office 02 Albacete http://www.catastro~ 0200~ 2022-03-14 00:00:00
 9 Territorial office 02 Albacete http://www.catastro~ 0201~ 2022-03-14 00:00:00
10 Territorial office 02 Albacete http://www.catastro~ 0201~ 2022-03-14 00:00:00
library(XML)
library(curl)

# install.packages("RCurl")
library(RCurl)
library(xml2)

# Download XML file from URL
url <- read_xml("http://www.catastro.minhap.es/INSPIRE/addresses/02/ES.SDGC.ad.atom_02.xml")
url
{xml_document}
<feed lang="en" xmlns="http://www.w3.org/2005/Atom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:georss="http://www.georss.org/georss" xmlns:inspire_dls="http://inspire.ec.europa.eu/schemas/inspire_dls/1.0">
 [1] <title>Download service of Addresses. Territorial Office 02 - Albacete</ ...
 [2] <subtitle>INSPIRE Addresses. Spanish Directorate General for Cadastre</s ...
 [3] <link href="http://www.catastro.minhap.es/INSPIRE/Addresses/ES.SDGC.AD.D ...
 [4] <link href="http://www.catastro.minhap.es/INSPIRE/Addresses/02/ES.SDGC.A ...
 [5] <link href="http://www.catastro.minhap.es/" rel="alternate" type="text/h ...
 [6] <id>http://www.catastro.minhap.es/INSPIRE/Addresses/02/ES.SDGC.AD.atom_0 ...
 [7] <rights>This service can be used free of charge in every instance,  as l ...
 [8] <updated>2022-03-11T00:00:00Z</updated>
 [9] <author>\n  <name>productosyservicios</name>\n  <email>productosyservici ...
[10] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02001-ABENG ...
[11] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02002-ALATO ...
[12] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02900-ALBAC ...
[13] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02004-ALBAT ...
[14] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02005-ALBOR ...
[15] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02006-ALCAD ...
[16] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02007-ALCAL ...
[17] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02008-ALCAR ...
[18] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02009-ALMAN ...
[19] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02010-ALPER ...
[20] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02011-AYNA  ...
...
xml_name(url)
[1] "feed"
xml_children(url)
{xml_nodeset (96)}
 [1] <title>Download service of Addresses. Territorial Office 02 - Albacete</ ...
 [2] <subtitle>INSPIRE Addresses. Spanish Directorate General for Cadastre</s ...
 [3] <link href="http://www.catastro.minhap.es/INSPIRE/Addresses/ES.SDGC.AD.D ...
 [4] <link href="http://www.catastro.minhap.es/INSPIRE/Addresses/02/ES.SDGC.A ...
 [5] <link href="http://www.catastro.minhap.es/" rel="alternate" type="text/h ...
 [6] <id>http://www.catastro.minhap.es/INSPIRE/Addresses/02/ES.SDGC.AD.atom_0 ...
 [7] <rights>This service can be used free of charge in every instance,  as l ...
 [8] <updated>2022-03-11T00:00:00Z</updated>
 [9] <author>\n  <name>productosyservicios</name>\n  <email>productosyservici ...
[10] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02001-ABENG ...
[11] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02002-ALATO ...
[12] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02900-ALBAC ...
[13] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02004-ALBAT ...
[14] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02005-ALBOR ...
[15] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02006-ALCAD ...
[16] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02007-ALCAL ...
[17] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02008-ALCAR ...
[18] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02009-ALMAN ...
[19] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02010-ALPER ...
[20] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02011-AYNA  ...
...
xml_set_text(url, "//http")
{xml_document}
<feed lang="en" xmlns="http://www.w3.org/2005/Atom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:georss="http://www.georss.org/georss" xmlns:inspire_dls="http://inspire.ec.europa.eu/schemas/inspire_dls/1.0">
 [1] <title>//http</title>
 [2] <subtitle>INSPIRE Addresses. Spanish Directorate General for Cadastre</s ...
 [3] <link href="http://www.catastro.minhap.es/INSPIRE/Addresses/ES.SDGC.AD.D ...
 [4] <link href="http://www.catastro.minhap.es/INSPIRE/Addresses/02/ES.SDGC.A ...
 [5] <link href="http://www.catastro.minhap.es/" rel="alternate" type="text/h ...
 [6] <id>http://www.catastro.minhap.es/INSPIRE/Addresses/02/ES.SDGC.AD.atom_0 ...
 [7] <rights>This service can be used free of charge in every instance,  as l ...
 [8] <updated>2022-03-11T00:00:00Z</updated>
 [9] <author>\n  <name>productosyservicios</name>\n  <email>productosyservici ...
[10] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02001-ABENG ...
[11] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02002-ALATO ...
[12] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02900-ALBAC ...
[13] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02004-ALBAT ...
[14] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02005-ALBOR ...
[15] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02006-ALCAD ...
[16] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02007-ALCAL ...
[17] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02008-ALCAR ...
[18] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02009-ALMAN ...
[19] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02010-ALPER ...
[20] <entry>\n  <!-- title for pre-defined dataset -->\n  <title> 02011-AYNA  ...
...

txt 641

library(spanish)
diremuni <- catr_atom_get_address(
  "Galapagar",
  to = "Madrid"
)
diremuni
Simple feature collection with 9589 features and 11 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 411842.7 ymin: 4485614 xmax: 420254.4 ymax: 4498194
Projected CRS: ETRS89 / UTM zone 30N
First 10 features:
                                   gml_id                     localId
1   ES.SDGC.AD.28.061.1.12.5124802VK1952S  28.061.1.12.5124802VK1952S
2   ES.SDGC.AD.28.061.1.16.5124803VK1952S  28.061.1.16.5124803VK1952S
3   ES.SDGC.AD.28.061.1.18.5124803VK1952S  28.061.1.18.5124803VK1952S
4   ES.SDGC.AD.28.061.1.1A.5123902VK1952S  28.061.1.1A.5123902VK1952S
5   ES.SDGC.AD.28.061.1.1B.5123902VK1952S  28.061.1.1B.5123902VK1952S
6    ES.SDGC.AD.28.061.1.3.5123901VK1952S   28.061.1.3.5123901VK1952S
7    ES.SDGC.AD.28.061.1.5.5123901VK1952S   28.061.1.5.5123901VK1952S
8    ES.SDGC.AD.28.061.1.7.5123901VK1952S   28.061.1.7.5123901VK1952S
9    ES.SDGC.AD.28.061.1.9.5123901VK1952S   28.061.1.9.5123901VK1952S
10 ES.SDGC.AD.28.061.100.1.5920209VK1952S 28.061.100.1.5920209VK1952S
    namespace specification      method default designator type     level
1  ES.SDGC.AD      Entrance fromFeature    TRUE         12    1 siteLevel
2  ES.SDGC.AD      Entrance fromFeature    TRUE         16    1 siteLevel
3  ES.SDGC.AD      Entrance fromFeature    TRUE         18    1 siteLevel
4  ES.SDGC.AD        Parcel fromFeature    TRUE         1A    1 siteLevel
5  ES.SDGC.AD        Parcel fromFeature    TRUE         1B    1 siteLevel
6  ES.SDGC.AD      Entrance fromFeature    TRUE          3    1 siteLevel
7  ES.SDGC.AD      Entrance fromFeature    TRUE          5    1 siteLevel
8  ES.SDGC.AD      Entrance fromFeature    TRUE          7    1 siteLevel
9  ES.SDGC.AD        Parcel fromFeature    TRUE          9    1 siteLevel
10 ES.SDGC.AD      Entrance fromFeature    TRUE          1    1 siteLevel
   validFrom beginLifespanVersion                 geometry
1       <NA>  2002-05-28T00:00:00 POINT (415049.3 4492199)
2       <NA>  2011-04-27T00:00:00 POINT (415036.7 4492189)
3       <NA>  2011-04-27T00:00:00 POINT (415023.3 4492179)
4       <NA>  2006-08-03T00:00:00 POINT (415075.5 4492168)
5       <NA>  2006-08-03T00:00:00 POINT (415075.5 4492168)
6       <NA>  2006-08-03T00:00:00 POINT (415065.9 4492176)
7       <NA>  2006-08-03T00:00:00 POINT (415047.9 4492172)
8       <NA>  2006-08-03T00:00:00 POINT (415037.1 4492164)
9       <NA>  2006-08-03T00:00:00 POINT (415044.5 4492160)
10      <NA>  2014-06-10T00:00:00 POINT (415738.2 4491912)
tmp_territorial <- read_html(territorial_office$url[[1]]) #lee la primera direccion xml
tmp_territorial
{html_document}
<html>
[1] <body><feed xmlns="http://www.w3.org/2005/Atom" xmlns:xsi="http://www.w3. ...

datos espaciales de todos los edificios pertenecientes a un mismo municipio

buildings_all<-catr_atom_get_buildings_db_all(
cache = TRUE,
update_cache = FALSE,
cache_dir = NULL,
verbose = FALSE
)
head(buildings_all, 5)
# A tibble: 5 x 4
  territorial_office             url                   munic date               
  <chr>                          <chr>                 <chr> <dttm>             
1 Territorial office 02 Albacete http://www.catastro.~ 0200~ 2022-03-14 00:00:00
2 Territorial office 02 Albacete http://www.catastro.~ 0200~ 2022-03-14 00:00:00
3 Territorial office 02 Albacete http://www.catastro.~ 0200~ 2022-03-14 00:00:00
4 Territorial office 02 Albacete http://www.catastro.~ 0200~ 2022-03-14 00:00:00
5 Territorial office 02 Albacete http://www.catastro.~ 0200~ 2022-03-14 00:00:00
tail(buildings_all, 5)
# A tibble: 5 x 4
  territorial_office            url                    munic date               
  <chr>                         <chr>                  <chr> <dttm>             
1 Territorial office 54 Vigo    http://www.catastro.m~ 5402~ 2022-03-14 00:00:00
2 Territorial office 54 Vigo    http://www.catastro.m~ 5403~ 2022-03-14 00:00:00
3 Territorial office 54 Vigo    http://www.catastro.m~ 5405~ 2022-03-14 00:00:00
4 Territorial office 55 Ceuta   http://www.catastro.m~ 5510~ 2022-03-14 00:00:00
5 Territorial office 56 Melilla http://www.catastro.m~ 5610~ 2022-03-14 00:00:00

datos espaciales de todas las parcelas pertenecientes a un mismo municipio

parcels_db_all<-catr_atom_get_parcels_db_all(
cache = TRUE,
update_cache = FALSE,
cache_dir = NULL,
verbose = FALSE
)

head(parcels_db_all, 5)
# A tibble: 5 x 4
  territorial_office             url                   munic date               
  <chr>                          <chr>                 <chr> <dttm>             
1 Territorial office 02 Albacete http://www.catastro.~ 0200~ 2022-03-14 00:00:00
2 Territorial office 02 Albacete http://www.catastro.~ 0200~ 2022-03-14 00:00:00
3 Territorial office 02 Albacete http://www.catastro.~ 0200~ 2022-03-14 00:00:00
4 Territorial office 02 Albacete http://www.catastro.~ 0200~ 2022-03-14 00:00:00
5 Territorial office 02 Albacete http://www.catastro.~ 0200~ 2022-03-14 00:00:00
tail(parcels_db_all, 5)
# A tibble: 5 x 4
  territorial_office            url                    munic date               
  <chr>                         <chr>                  <chr> <dttm>             
1 Territorial office 54 Vigo    http://www.catastro.m~ 5402~ 2022-03-14 00:00:00
2 Territorial office 54 Vigo    http://www.catastro.m~ 5403~ 2022-03-14 00:00:00
3 Territorial office 54 Vigo    http://www.catastro.m~ 5405~ 2022-03-14 00:00:00
4 Territorial office 55 Ceuta   http://www.catastro.m~ 5510~ 2022-03-14 00:00:00
5 Territorial office 56 Melilla http://www.catastro.m~ 5610~ 2022-03-14 00:00:00
# usando con coordenadas
code_from_coords<-catr_get_code_from_coords(c(-16.25462, 28.46824), srs = 4326)
t(code_from_coords)
           [,1]                    
munic      "SANTA CRUZ DE TENERIFE"
catr_to    "38"                    
catr_munic "900"                   
catrcode   "38900"                 
cpro       "38"                    
cmun       "038"                   
inecode    "38038"                 
nm         "SANTA CRUZ DE TENERIFE"
cd         "38"                    
cmc        "900"                   
cp         "38"                    
cm         "38"                    
# usando sf
prov <- mapSpain::esp_get_prov("Caceres")
code_from_coords1<-catr_get_code_from_coords(prov)
t(code_from_coords1)
           [,1]    
munic      "MONROY"
catr_to    "10"    
catr_munic "128"   
catrcode   "10128" 
cpro       "10"    
cmun       "125"   
inecode    "10125" 
nm         "MONROY"
cd         "10"    
cmc        "128"   
cp         "10"    
cm         "125"   
munic<-catr_atom_get_address_db_to(
to = "Madrid",
cache = TRUE,
update_cache = FALSE,
cache_dir = NULL,
verbose = FALSE
)

munic
# A tibble: 179 x 3
   munic                             url                     date               
   <chr>                             <chr>                   <dttm>             
 1 28002-AJALVIR addresses           http://www.catastro.mi~ 2022-03-11 00:00:00
 2 28003-ALAMEDA DEL VALLE addresses http://www.catastro.mi~ 2022-03-11 00:00:00
 3 28005-ALCALA DE HENARES addresses http://www.catastro.mi~ 2022-03-11 00:00:00
 4 28006-ALCOBENDAS addresses        http://www.catastro.mi~ 2022-03-11 00:00:00
 5 28007-ALCORCON addresses          http://www.catastro.mi~ 2022-03-11 00:00:00
 6 28008-ALDEA DEL FRESNO addresses  http://www.catastro.mi~ 2022-03-11 00:00:00
 7 28009-ALGETE addresses            http://www.catastro.mi~ 2022-03-11 00:00:00
 8 28010-ALPEDRETE addresses         http://www.catastro.mi~ 2022-03-11 00:00:00
 9 28011-AMBITE addresses            http://www.catastro.mi~ 2022-03-11 00:00:00
10 28012-ANCHUELO addresses          http://www.catastro.mi~ 2022-03-11 00:00:00
# ... with 169 more rows
head(munic$url,3)
[1] "http://www.catastro.minhap.es/INSPIRE/Addresses/28/28002-AJALVIR/A.ES.SDGC.AD.28002.zip"          
[2] "http://www.catastro.minhap.es/INSPIRE/Addresses/28/28003-ALAMEDA DEL VALLE/A.ES.SDGC.AD.28003.zip"
[3] "http://www.catastro.minhap.es/INSPIRE/Addresses/28/28005-ALCALA DE HENARES/A.ES.SDGC.AD.28005.zip"
tail(munic$url,3)
[1] "http://www.catastro.minhap.es/INSPIRE/Addresses/28/28181-VILLAVICIOSA DE ODON/A.ES.SDGC.AD.28181.zip" 
[2] "http://www.catastro.minhap.es/INSPIRE/Addresses/28/28182-VILLAVIEJA DEL LOZOYA/A.ES.SDGC.AD.28182.zip"
[3] "http://www.catastro.minhap.es/INSPIRE/Addresses/28/28183-ZARZALEJO/A.ES.SDGC.AD.28183.zip"            
url <- ( "http://www.catastro.minhap.es/INSPIRE/Addresses/28/28044-COLMENAREJO/A.ES.SDGC.AD.28044.zip")

destination<- "C:/Users/polo/OneDrive/ZZZ/vivienda/catastro obtenido de r//A.ES.SDGC.AD.28044.zip"

download.file(url, destination)
buildings<-catr_atom_get_buildings_db_to(
  to = "Madrid",
  cache = TRUE,
  update_cache = FALSE,
  cache_dir = NULL,
  verbose = FALSE
)
buildings
# A tibble: 179 x 3
   munic                             url                     date               
   <chr>                             <chr>                   <dttm>             
 1 28002-AJALVIR buildings           http://www.catastro.mi~ 2022-03-11 00:00:00
 2 28003-ALAMEDA DEL VALLE buildings http://www.catastro.mi~ 2022-03-11 00:00:00
 3 28005-ALCALA DE HENARES buildings http://www.catastro.mi~ 2022-03-11 00:00:00
 4 28006-ALCOBENDAS buildings        http://www.catastro.mi~ 2022-03-11 00:00:00
 5 28007-ALCORCON buildings          http://www.catastro.mi~ 2022-03-11 00:00:00
 6 28008-ALDEA DEL FRESNO buildings  http://www.catastro.mi~ 2022-03-11 00:00:00
 7 28009-ALGETE buildings            http://www.catastro.mi~ 2022-03-11 00:00:00
 8 28010-ALPEDRETE buildings         http://www.catastro.mi~ 2022-03-11 00:00:00
 9 28011-AMBITE buildings            http://www.catastro.mi~ 2022-03-11 00:00:00
10 28012-ANCHUELO buildings          http://www.catastro.mi~ 2022-03-11 00:00:00
# ... with 169 more rows
buildings$url
  [1] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28002-AJALVIR/A.ES.SDGC.BU.28002.zip"                       
  [2] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28003-ALAMEDA DEL VALLE/A.ES.SDGC.BU.28003.zip"             
  [3] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28005-ALCALA DE HENARES/A.ES.SDGC.BU.28005.zip"             
  [4] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28006-ALCOBENDAS/A.ES.SDGC.BU.28006.zip"                    
  [5] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28007-ALCORCON/A.ES.SDGC.BU.28007.zip"                      
  [6] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28008-ALDEA DEL FRESNO/A.ES.SDGC.BU.28008.zip"              
  [7] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28009-ALGETE/A.ES.SDGC.BU.28009.zip"                        
  [8] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28010-ALPEDRETE/A.ES.SDGC.BU.28010.zip"                     
  [9] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28011-AMBITE/A.ES.SDGC.BU.28011.zip"                        
 [10] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28012-ANCHUELO/A.ES.SDGC.BU.28012.zip"                      
 [11] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28013-ARANJUEZ/A.ES.SDGC.BU.28013.zip"                      
 [12] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28014-ARGANDA DEL REY/A.ES.SDGC.BU.28014.zip"               
 [13] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28015-ARROYOMOLINOS/A.ES.SDGC.BU.28015.zip"                 
 [14] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28017-BATRES/A.ES.SDGC.BU.28017.zip"                        
 [15] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28018-BECERRIL DE LA SIERRA/A.ES.SDGC.BU.28018.zip"         
 [16] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28019-BELMONTE DE TAJO/A.ES.SDGC.BU.28019.zip"              
 [17] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28021-BERZOSA DEL LOZOYA/A.ES.SDGC.BU.28021.zip"            
 [18] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28022-BOADILLA DEL MONTE/A.ES.SDGC.BU.28022.zip"            
 [19] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28024-BRAOJOS/A.ES.SDGC.BU.28024.zip"                       
 [20] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28025-BREA DE TAJO/A.ES.SDGC.BU.28025.zip"                  
 [21] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28026-BRUNETE/A.ES.SDGC.BU.28026.zip"                       
 [22] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28027-BUITRAGO DEL LOZOYA/A.ES.SDGC.BU.28027.zip"           
 [23] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28028-BUSTARVIEJO/A.ES.SDGC.BU.28028.zip"                   
 [24] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28029-CABANILLAS DE LA SIERRA/A.ES.SDGC.BU.28029.zip"       
 [25] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28031-CADALSO DE LOS VIDRIOS/A.ES.SDGC.BU.28031.zip"        
 [26] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28032-CAMARMA DE ESTERUELAS/A.ES.SDGC.BU.28032.zip"         
 [27] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28033-CAMPO REAL/A.ES.SDGC.BU.28033.zip"                    
 [28] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28034-CANENCIA/A.ES.SDGC.BU.28034.zip"                      
 [29] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28035-CARABAÑA/A.ES.SDGC.BU.28035.zip"                      
 [30] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28036-CASARRUBUELOS/A.ES.SDGC.BU.28036.zip"                 
 [31] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28037-CENICIENTOS/A.ES.SDGC.BU.28037.zip"                   
 [32] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28038-CERCEDILLA/A.ES.SDGC.BU.28038.zip"                    
 [33] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28039-CERVERA DE BUITRAGO/A.ES.SDGC.BU.28039.zip"           
 [34] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28051-CHAPINERIA/A.ES.SDGC.BU.28051.zip"                    
 [35] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28052-CHINCHON/A.ES.SDGC.BU.28052.zip"                      
 [36] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28040-CIEMPOZUELOS/A.ES.SDGC.BU.28040.zip"                  
 [37] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28041-COBEÑA/A.ES.SDGC.BU.28041.zip"                        
 [38] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28046-COLLADO MEDIANO/A.ES.SDGC.BU.28046.zip"               
 [39] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28047-COLLADO VILLALBA/A.ES.SDGC.BU.28047.zip"              
 [40] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28043-COLMENAR DE OREJA/A.ES.SDGC.BU.28043.zip"             
 [41] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28042-COLMENAR DEL ARROYO/A.ES.SDGC.BU.28042.zip"           
 [42] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28045-COLMENAR VIEJO/A.ES.SDGC.BU.28045.zip"                
 [43] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28044-COLMENAREJO/A.ES.SDGC.BU.28044.zip"                   
 [44] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28048-CORPA/A.ES.SDGC.BU.28048.zip"                         
 [45] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28049-COSLADA/A.ES.SDGC.BU.28049.zip"                       
 [46] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28050-CUBAS DE LA SAGRA/A.ES.SDGC.BU.28050.zip"             
 [47] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28053-DAGANZO DE ARRIBA/A.ES.SDGC.BU.28053.zip"             
 [48] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28004-EL ALAMO/A.ES.SDGC.BU.28004.zip"                      
 [49] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28016-EL ATAZAR/A.ES.SDGC.BU.28016.zip"                     
 [50] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28020-EL BERRUECO/A.ES.SDGC.BU.28020.zip"                   
 [51] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28023-EL BOALO/A.ES.SDGC.BU.28023.zip"                      
 [52] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28054-EL ESCORIAL/A.ES.SDGC.BU.28054.zip"                   
 [53] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28086-EL MOLAR/A.ES.SDGC.BU.28086.zip"                      
 [54] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28168-EL VELLON/A.ES.SDGC.BU.28168.zip"                     
 [55] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28055-ESTREMERA/A.ES.SDGC.BU.28055.zip"                     
 [56] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28056-FRESNEDILLAS DE LA OLIVA/A.ES.SDGC.BU.28056.zip"      
 [57] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28057-FRESNO DE TOROTE/A.ES.SDGC.BU.28057.zip"              
 [58] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28058-FUENLABRADA/A.ES.SDGC.BU.28058.zip"                   
 [59] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28059-FUENTE EL SAZ DE JARAMA/A.ES.SDGC.BU.28059.zip"       
 [60] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28060-FUENTIDUEÑA DE TAJO/A.ES.SDGC.BU.28060.zip"           
 [61] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28061-GALAPAGAR/A.ES.SDGC.BU.28061.zip"                     
 [62] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28062-GARGANTA DE LOS MONTES/A.ES.SDGC.BU.28062.zip"        
 [63] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28063-GARGANTILLA DEL LOZOYA Y PINIL/A.ES.SDGC.BU.28063.zip"
 [64] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28064-GASCONES/A.ES.SDGC.BU.28064.zip"                      
 [65] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28065-GETAFE/A.ES.SDGC.BU.28065.zip"                        
 [66] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28066-GRIÑON/A.ES.SDGC.BU.28066.zip"                        
 [67] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28067-GUADALIX DE LA SIERRA/A.ES.SDGC.BU.28067.zip"         
 [68] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28068-GUADARRAMA/A.ES.SDGC.BU.28068.zip"                    
 [69] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28070-HORCAJO DE LA SIERRA AOSLOS/A.ES.SDGC.BU.28070.zip"   
 [70] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28071-HORCAJUELO DE LA SIERRA/A.ES.SDGC.BU.28071.zip"       
 [71] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28072-HOYO DE MANZANARES/A.ES.SDGC.BU.28072.zip"            
 [72] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28073-HUMANES DE MADRID/A.ES.SDGC.BU.28073.zip"             
 [73] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28001-LA ACEBEDA/A.ES.SDGC.BU.28001.zip"                    
 [74] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28030-LA CABRERA/A.ES.SDGC.BU.28030.zip"                    
 [75] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28069-LA HIRUELA/A.ES.SDGC.BU.28069.zip"                    
 [76] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28138-LA SERNA DEL MONTE/A.ES.SDGC.BU.28138.zip"            
 [77] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28127-LAS ROZAS DE MADRID/A.ES.SDGC.BU.28127.zip"           
 [78] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28074-LEGANES/A.ES.SDGC.BU.28074.zip"                       
 [79] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28075-LOECHES/A.ES.SDGC.BU.28075.zip"                       
 [80] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28087-LOS MOLINOS/A.ES.SDGC.BU.28087.zip"                   
 [81] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28137-LOS SANTOS DE LA HUMOSA/A.ES.SDGC.BU.28137.zip"       
 [82] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28076-LOZOYA/A.ES.SDGC.BU.28076.zip"                        
 [83] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28077-LOZOYUELA NAVAS SIETEIGLESIAS/A.ES.SDGC.BU.28077.zip" 
 [84] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28078-MADARCOS/A.ES.SDGC.BU.28078.zip"                      
 [85] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28900-MADRID/A.ES.SDGC.BU.28900.zip"                        
 [86] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28080-MAJADAHONDA/A.ES.SDGC.BU.28080.zip"                   
 [87] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28082-MANZANARES EL REAL/A.ES.SDGC.BU.28082.zip"            
 [88] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28083-MECO/A.ES.SDGC.BU.28083.zip"                          
 [89] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28084-MEJORADA DEL CAMPO/A.ES.SDGC.BU.28084.zip"            
 [90] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28085-MIRAFLORES DE LA SIERRA/A.ES.SDGC.BU.28085.zip"       
 [91] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28088-MONTEJO DE LA SIERRA/A.ES.SDGC.BU.28088.zip"          
 [92] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28089-MORALEJA DE ENMEDIO/A.ES.SDGC.BU.28089.zip"           
 [93] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28090-MORALZARZAL/A.ES.SDGC.BU.28090.zip"                   
 [94] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28091-MORATA DE TAJUÑA/A.ES.SDGC.BU.28091.zip"              
 [95] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28092-MOSTOLES/A.ES.SDGC.BU.28092.zip"                      
 [96] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28093-NAVACERRADA/A.ES.SDGC.BU.28093.zip"                   
 [97] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28094-NAVALAFUENTE/A.ES.SDGC.BU.28094.zip"                  
 [98] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28095-NAVALAGAMELLA/A.ES.SDGC.BU.28095.zip"                 
 [99] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28096-NAVALCARNERO/A.ES.SDGC.BU.28096.zip"                  
[100] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28097-NAVARREDONDA Y SAN MAMES/A.ES.SDGC.BU.28097.zip"      
[101] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28099-NAVAS DEL REY/A.ES.SDGC.BU.28099.zip"                 
[102] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28100-NUEVO BAZTAN/A.ES.SDGC.BU.28100.zip"                  
[103] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28101-OLMEDA DE LAS FUENTES/A.ES.SDGC.BU.28101.zip"         
[104] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28102-ORUSCO DE TAJUÑA/A.ES.SDGC.BU.28102.zip"              
[105] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28104-PARACUELLOS DE JARAMA/A.ES.SDGC.BU.28104.zip"         
[106] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28106-PARLA/A.ES.SDGC.BU.28106.zip"                         
[107] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28107-PATONES/A.ES.SDGC.BU.28107.zip"                       
[108] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28108-PEDREZUELA/A.ES.SDGC.BU.28108.zip"                    
[109] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28109-PELAYOS DE LA PRESA/A.ES.SDGC.BU.28109.zip"           
[110] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28110-PERALES DE TAJUÑA/A.ES.SDGC.BU.28110.zip"             
[111] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28111-PEZUELA DE LAS TORRES/A.ES.SDGC.BU.28111.zip"         
[112] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28112-PINILLA DEL VALLE/A.ES.SDGC.BU.28112.zip"             
[113] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28113-PINTO/A.ES.SDGC.BU.28113.zip"                         
[114] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28114-PIÑUECAR GANDULLAS/A.ES.SDGC.BU.28114.zip"            
[115] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28115-POZUELO DE ALARCON/A.ES.SDGC.BU.28115.zip"            
[116] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28116-POZUELO DEL REY/A.ES.SDGC.BU.28116.zip"               
[117] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28117-PRADENA DEL RINCON/A.ES.SDGC.BU.28117.zip"            
[118] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28118-PUEBLA DE LA SIERRA/A.ES.SDGC.BU.28118.zip"           
[119] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28185-PUENTES VIEJAS/A.ES.SDGC.BU.28185.zip"                
[120] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28119-QUIJORNA/A.ES.SDGC.BU.28119.zip"                      
[121] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28120-RASCAFRIA/A.ES.SDGC.BU.28120.zip"                     
[122] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28121-REDUEÑA/A.ES.SDGC.BU.28121.zip"                       
[123] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28122-RIBATEJADA/A.ES.SDGC.BU.28122.zip"                    
[124] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28123-RIVAS VACIAMADRID/A.ES.SDGC.BU.28123.zip"             
[125] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28124-ROBLEDILLO DE LA JARA/A.ES.SDGC.BU.28124.zip"         
[126] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28125-ROBLEDO DE CHAVELA/A.ES.SDGC.BU.28125.zip"            
[127] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28126-ROBREGORDO/A.ES.SDGC.BU.28126.zip"                    
[128] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28128-ROZAS DE PUERTO REAL/A.ES.SDGC.BU.28128.zip"          
[129] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28129-SAN AGUSTIN DEL GUADALIX/A.ES.SDGC.BU.28129.zip"      
[130] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28130-SAN FERNANDO DE HENARES/A.ES.SDGC.BU.28130.zip"       
[131] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28131-SAN LORENZO DE EL ESCORIAL/A.ES.SDGC.BU.28131.zip"    
[132] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28132-SAN MARTIN DE LA VEGA/A.ES.SDGC.BU.28132.zip"         
[133] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28133-SAN MARTIN DE VALDEIGLESIAS/A.ES.SDGC.BU.28133.zip"   
[134] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28134-SAN SEBASTIAN DE LOS REYES/A.ES.SDGC.BU.28134.zip"    
[135] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28135-SANTA MARIA DE LA ALAMEDA/A.ES.SDGC.BU.28135.zip"     
[136] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28136-SANTORCAZ/A.ES.SDGC.BU.28136.zip"                     
[137] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28140-SERRANILLOS DEL VALLE/A.ES.SDGC.BU.28140.zip"         
[138] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28141-SEVILLA LA NUEVA/A.ES.SDGC.BU.28141.zip"              
[139] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28143-SOMOSIERRA/A.ES.SDGC.BU.28143.zip"                    
[140] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28144-SOTO DEL REAL/A.ES.SDGC.BU.28144.zip"                 
[141] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28145-TALAMANCA DE JARAMA/A.ES.SDGC.BU.28145.zip"           
[142] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28146-TIELMES/A.ES.SDGC.BU.28146.zip"                       
[143] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28147-TITULCIA/A.ES.SDGC.BU.28147.zip"                      
[144] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28148-TORREJON DE ARDOZ/A.ES.SDGC.BU.28148.zip"             
[145] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28149-TORREJON DE LA CALZADA/A.ES.SDGC.BU.28149.zip"        
[146] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28150-TORREJON DE VELASCO/A.ES.SDGC.BU.28150.zip"           
[147] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28151-TORRELAGUNA/A.ES.SDGC.BU.28151.zip"                   
[148] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28152-TORRELODONES/A.ES.SDGC.BU.28152.zip"                  
[149] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28153-TORREMOCHA DE JARAMA/A.ES.SDGC.BU.28153.zip"          
[150] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28154-TORRES DE LA ALAMEDA/A.ES.SDGC.BU.28154.zip"          
[151] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28184-TRES CANTOS/A.ES.SDGC.BU.28184.zip"                   
[152] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28155-VALDARACETE/A.ES.SDGC.BU.28155.zip"                   
[153] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28156-VALDEAVERO/A.ES.SDGC.BU.28156.zip"                    
[154] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28157-VALDELAGUNA/A.ES.SDGC.BU.28157.zip"                   
[155] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28158-VALDEMANCO/A.ES.SDGC.BU.28158.zip"                    
[156] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28159-VALDEMAQUEDA/A.ES.SDGC.BU.28159.zip"                  
[157] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28160-VALDEMORILLO/A.ES.SDGC.BU.28160.zip"                  
[158] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28161-VALDEMORO/A.ES.SDGC.BU.28161.zip"                     
[159] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28162-VALDEOLMOS ALALPARDO/A.ES.SDGC.BU.28162.zip"          
[160] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28163-VALDEPIELAGOS/A.ES.SDGC.BU.28163.zip"                 
[161] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28164-VALDETORRES DE JARAMA/A.ES.SDGC.BU.28164.zip"         
[162] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28165-VALDILECHA/A.ES.SDGC.BU.28165.zip"                    
[163] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28166-VALVERDE DE ALCALA/A.ES.SDGC.BU.28166.zip"            
[164] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28167-VELILLA DE SAN ANTONIO/A.ES.SDGC.BU.28167.zip"        
[165] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28169-VENTURADA/A.ES.SDGC.BU.28169.zip"                     
[166] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28171-VILLA DEL PRADO/A.ES.SDGC.BU.28171.zip"               
[167] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28170-VILLACONEJOS/A.ES.SDGC.BU.28170.zip"                  
[168] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28172-VILLALBILLA/A.ES.SDGC.BU.28172.zip"                   
[169] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28173-VILLAMANRIQUE DE TAJO/A.ES.SDGC.BU.28173.zip"         
[170] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28174-VILLAMANTA/A.ES.SDGC.BU.28174.zip"                    
[171] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28175-VILLAMANTILLA/A.ES.SDGC.BU.28175.zip"                 
[172] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28176-VILLANUEVA DE LA CAÑADA/A.ES.SDGC.BU.28176.zip"       
[173] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28178-VILLANUEVA DE PERALES/A.ES.SDGC.BU.28178.zip"         
[174] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28177-VILLANUEVA DEL PARDILLO/A.ES.SDGC.BU.28177.zip"       
[175] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28179-VILLAR DEL OLMO/A.ES.SDGC.BU.28179.zip"               
[176] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28180-VILLAREJO DE SALVANES/A.ES.SDGC.BU.28180.zip"         
[177] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28181-VILLAVICIOSA DE ODON/A.ES.SDGC.BU.28181.zip"          
[178] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28182-VILLAVIEJA DEL LOZOYA/A.ES.SDGC.BU.28182.zip"         
[179] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28183-ZARZALEJO/A.ES.SDGC.BU.28183.zip"                     
urlB <- ( "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28047-COLLADO VILLALBA/A.ES.SDGC.BU.28047.zip")

destination<- "C:/Users/polo/OneDrive/ZZZ/vivienda/catastro obtenido de r/A.ES.SDGC.BU.28047.zip"

download.file(urlB, destination)


# https://rdrr.io/github/fisabio/medear/man/descarga_cartografia.html

# if (!"devtools" %in% installed.packages())
#   install.packages("devtools")
# devtools::install_github("fisabio/medear", build_opts = c("--no-resave-data", "--no-manual"))

library(medear)

# trameros <- descarga_trameros(cod_provincia = c("51", "52"))
# trameros

carto_ine    <- descarga_cartografia(epsg = 4326, conservar = TRUE, ntries = 10)
carto_ine_46 <- carto_ine[substr(carto_ine$seccion, 3, 4) == "46", ]
plot(carto_ine_46)


Consultar en la Sede Electrónica
del Catastro la parcela:

3609501VK2931S0001BU

40° 33.901’ N 3° 54.058’ O 30 T 423730.73 m E 4490861.89 m N

LEER UN FICHERO SHP:

# leemos el archivo shp de los construcciones en galapagar
shp_galapagar <- read_sf("E:/archivo shp/GALAPAGAR/CONSTRU.SHP")
shp_galapagar
Simple feature collection with 111779 features and 21 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 411836.8 ymin: 4485597 xmax: 420313.7 ymax: 4498207
Projected CRS: ETRS89 / UTM zone 30N
# A tibble: 111,779 x 22
    MAPA DELEGACIO MUNICIPIO MASA  PARCELA HOJA    TIPO  CONSTRU   COORX   COORY
   <int>     <int>     <int> <chr> <chr>   <chr>   <chr> <chr>     <dbl>   <dbl>
 1 28235        28        61 25728 05      VK1927S U     POR+I   412471.  4.50e6
 2 28235        28        61 25728 06      VK1927S U     I       412448.  4.50e6
 3 28235        28        61 25728 06      VK1927S U     I       412459.  4.50e6
 4 28235        28        61 26792 07      VK1927N U     P       412517.  4.50e6
 5 28235        28        61 26792 07      VK1927N U     PI      412510.  4.50e6
 6 28235        28        61 26792 07      VK1927N U     TZA     412504.  4.50e6
 7 28235        28        61 26792 07      VK1927N U     TZA     412509.  4.50e6
 8 28235        28        61 26792 08      VK1927N U     I       412486.  4.50e6
 9 28235        28        61 26792 08      VK1927N U     II      412481.  4.50e6
10 28235        28        61 26792 08      VK1927N U     P       412478.  4.50e6
# ... with 111,769 more rows, and 12 more variables: NUMSYMBOL <int>,
#   AREA <dbl>, FECHAALTA <int>, FECHABAJA <int>, NINTERNO <dbl>, PCAT1 <chr>,
#   PCAT2 <chr>, EJERCICIO <int>, NUM_EXP <int>, CONTROL <int>, REFCAT <chr>,
#   geometry <POLYGON [m]>
prueba<-head(shp_galapagar, 50)
prueba
Simple feature collection with 50 features and 21 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 412430.4 ymin: 4492594 xmax: 413441 ymax: 4497797
Projected CRS: ETRS89 / UTM zone 30N
# A tibble: 50 x 22
    MAPA DELEGACIO MUNICIPIO MASA  PARCELA HOJA    TIPO  CONSTRU   COORX   COORY
   <int>     <int>     <int> <chr> <chr>   <chr>   <chr> <chr>     <dbl>   <dbl>
 1 28235        28        61 25728 05      VK1927S U     POR+I   412471.  4.50e6
 2 28235        28        61 25728 06      VK1927S U     I       412448.  4.50e6
 3 28235        28        61 25728 06      VK1927S U     I       412459.  4.50e6
 4 28235        28        61 26792 07      VK1927N U     P       412517.  4.50e6
 5 28235        28        61 26792 07      VK1927N U     PI      412510.  4.50e6
 6 28235        28        61 26792 07      VK1927N U     TZA     412504.  4.50e6
 7 28235        28        61 26792 07      VK1927N U     TZA     412509.  4.50e6
 8 28235        28        61 26792 08      VK1927N U     I       412486.  4.50e6
 9 28235        28        61 26792 08      VK1927N U     II      412481.  4.50e6
10 28235        28        61 26792 08      VK1927N U     P       412478.  4.50e6
# ... with 40 more rows, and 12 more variables: NUMSYMBOL <int>, AREA <dbl>,
#   FECHAALTA <int>, FECHABAJA <int>, NINTERNO <dbl>, PCAT1 <chr>, PCAT2 <chr>,
#   EJERCICIO <int>, NUM_EXP <int>, CONTROL <int>, REFCAT <chr>,
#   geometry <POLYGON [m]>
prueba$new<-lapply(prueba$REFCAT, catr_ovc_get_cpmrc)
prueba
Simple feature collection with 50 features and 22 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 412430.4 ymin: 4492594 xmax: 413441 ymax: 4497797
Projected CRS: ETRS89 / UTM zone 30N
# A tibble: 50 x 23
    MAPA DELEGACIO MUNICIPIO MASA  PARCELA HOJA    TIPO  CONSTRU   COORX   COORY
 * <int>     <int>     <int> <chr> <chr>   <chr>   <chr> <chr>     <dbl>   <dbl>
 1 28235        28        61 25728 05      VK1927S U     POR+I   412471.  4.50e6
 2 28235        28        61 25728 06      VK1927S U     I       412448.  4.50e6
 3 28235        28        61 25728 06      VK1927S U     I       412459.  4.50e6
 4 28235        28        61 26792 07      VK1927N U     P       412517.  4.50e6
 5 28235        28        61 26792 07      VK1927N U     PI      412510.  4.50e6
 6 28235        28        61 26792 07      VK1927N U     TZA     412504.  4.50e6
 7 28235        28        61 26792 07      VK1927N U     TZA     412509.  4.50e6
 8 28235        28        61 26792 08      VK1927N U     I       412486.  4.50e6
 9 28235        28        61 26792 08      VK1927N U     II      412481.  4.50e6
10 28235        28        61 26792 08      VK1927N U     P       412478.  4.50e6
# ... with 40 more rows, and 13 more variables: NUMSYMBOL <int>, AREA <dbl>,
#   FECHAALTA <int>, FECHABAJA <int>, NINTERNO <dbl>, PCAT1 <chr>, PCAT2 <chr>,
#   EJERCICIO <int>, NUM_EXP <int>, CONTROL <int>, REFCAT <chr>,
#   geometry <POLYGON [m]>, new <list>
prueba$new[[1]]
# A tibble: 1 x 10
  xcoord ycoord refcat     address pc.pc1 pc.pc2 geo.xcen geo.ycen geo.srs ldt  
   <dbl>  <dbl> <chr>      <chr>   <chr>  <chr>  <chr>    <chr>    <chr>   <chr>
1  -4.03   40.6 2572805VK~ CL SAN~ 25728~ VK192~ -4.0348~ 40.6188~ EPSG:4~ CL S~
prueba$new[[2]]
# A tibble: 1 x 10
  xcoord ycoord refcat     address pc.pc1 pc.pc2 geo.xcen geo.ycen geo.srs ldt  
   <dbl>  <dbl> <chr>      <chr>   <chr>  <chr>  <chr>    <chr>    <chr>   <chr>
1  -4.04   40.6 2572806VK~ CL SAN~ 25728~ VK192~ -4.0350~ 40.6190~ EPSG:4~ CL S~
rbind(prueba$new[[1]], prueba$new[[2]], prueba$new[[3]])
# A tibble: 3 x 10
  xcoord ycoord refcat     address pc.pc1 pc.pc2 geo.xcen geo.ycen geo.srs ldt  
   <dbl>  <dbl> <chr>      <chr>   <chr>  <chr>  <chr>    <chr>    <chr>   <chr>
1  -4.03   40.6 2572805VK~ CL SAN~ 25728~ VK192~ -4.0348~ 40.6188~ EPSG:4~ CL S~
2  -4.04   40.6 2572806VK~ CL SAN~ 25728~ VK192~ -4.0350~ 40.6190~ EPSG:4~ CL S~
3  -4.04   40.6 2572806VK~ CL SAN~ 25728~ VK192~ -4.0350~ 40.6190~ EPSG:4~ CL S~

MAPAS URBANO:

# devtools::install_github("datawookie/feedeR")
# install.packages("feedeR")
library(feedeR) # Importar formatos de redifusión RSS

url <- "http://www.catastro.minhap.es/INSPIRE/buildings/ES.SDGC.bu.atom.xml"

# importamos los RSS con enlaces por provincias
prov_enlaces <- feed.extract(url)

# Extraemos la tabla con los enlaces y limpiamos los titulos de espacios vacios
prov_enlaces_tab <- as_tibble(prov_enlaces$items)  %>%  
        mutate(title=stringr::str_trim(title))
head(prov_enlaces_tab)
# A tibble: 6 x 5
  title                          date                link      description hash 
  <chr>                          <dttm>              <chr>     <chr>       <chr>
1 Territorial office 02 Albacete 2022-03-14 00:00:00 http://w~ "\n\n\t\t ~ d21e~
2 Territorial office 03 Alicante 2022-03-14 00:00:00 http://w~ "\n\n\t\t ~ bdba~
3 Territorial office 04 Almería  2022-03-14 00:00:00 http://w~ "\n\n\t\t ~ 03bc~
4 Territorial office 05 Avila    2022-03-14 00:00:00 http://w~ "\n\n\t\t ~ 8a15~
5 Territorial office 06 Badajoz  2022-03-14 00:00:00 http://w~ "\n\n\t\t ~ 7d3f~
6 Territorial office 07 Baleares 2022-03-14 00:00:00 http://w~ "\n\n\t\t ~ 9c08~
# filtramos la provincia y obtenemos la url RSS que está en la columna link
# pull es lo mismo que $link en la data.frame

atom <- filter(prov_enlaces_tab, str_detect(title, "Madrid")) %>% pull(link)

# importamos la RSS de atom que nos da los enlaces municipales
enlaces <- feed.extract(atom) # ojo falla con UTF-8 en acentos

# vemos lo que contiene la tabla de municipios
enlaces$items
# A tibble: 179 x 5
   title                             date                link  description hash 
   <chr>                             <dttm>              <chr> <chr>       <chr>
 1 " 28002-AJALVIR buildings"        2022-03-11 00:00:00 http~ "\n\n\t\t ~ 1a19~
 2 " 28003-ALAMEDA DEL VALLE buildi~ 2022-03-11 00:00:00 http~ "\n\n\t\t ~ 0aca~
 3 " 28005-ALCALA DE HENARES buildi~ 2022-03-11 00:00:00 http~ "\n\n\t\t ~ 2ebc~
 4 " 28006-ALCOBENDAS buildings"     2022-03-11 00:00:00 http~ "\n\n\t\t ~ d03b~
 5 " 28007-ALCORCON buildings"       2022-03-11 00:00:00 http~ "\n\n\t\t ~ c93b~
 6 " 28008-ALDEA DEL FRESNO buildin~ 2022-03-11 00:00:00 http~ "\n\n\t\t ~ 84fd~
 7 " 28009-ALGETE buildings"         2022-03-11 00:00:00 http~ "\n\n\t\t ~ 57a8~
 8 " 28010-ALPEDRETE buildings"      2022-03-11 00:00:00 http~ "\n\n\t\t ~ db6a~
 9 " 28011-AMBITE buildings"         2022-03-11 00:00:00 http~ "\n\n\t\t ~ f539~
10 " 28012-ANCHUELO buildings"       2022-03-11 00:00:00 http~ "\n\n\t\t ~ 63ab~
# ... with 169 more rows
# Obtenemos la tabla con los enlaces de descarga
enlaces_tab <- enlaces$items

cp<-c("28061") # codigo del municipio de Galapagar

# filtramos la tabla con el nombre de la ciudad
link <- filter(enlaces_tab, str_detect(title, cp)) %>% pull(link)

# este sería el enlace final:
link
[1] "http://www.catastro.minhap.es/INSPIRE/Buildings/28/28061-GALAPAGAR/A.ES.SDGC.BU.28061.zip"

DESCARGA DE DATOS

# creamos un archivo temporal 
temp <- tempfile()

# descargamos los datos a ese fichero
download.file(URLencode(link), temp)

# descomprimimos a una carpeta llamada capas
unzip(temp, exdir = "capas")

Hemos descomprimido el zip del enlace en un directorio llamado capas, por lo que ahora tenemos 5 ficheros con datos espaciales. El que nos interesa es el denominado buildings.gml.

Para importar los datos utilizaremos la función dir_ls(ruta) del paquete fs que hace lo mismo que dir(ruta,full.names = T) de RBase, pero nos permite añadir un argumento de busqueda tipo expresion regular (regexp: expresión regular) por lo que en una línea obtenemos el nombre y ruta del fichero.

Aplicamos después la función st_read() del paquete sf que lee capas espaciales (kml, shp…), entre ellas está el formato Geography Markup Language (GML) que es el tipo del fichero que nos interesa hoy.

  library(fs)  # manejo de ficheros fs
# obtenemos la ruta con el archivo
file <- dir_ls("./capas", regexp = "A.ES.SDGC.BU.28061.building.gml")
  
# Leemos los datos espaciales en R
edificios <- st_read(file)
Reading layer `Building' from data source 
  `C:\Users\polo\OneDrive\ZZZ\vivienda\capas\A.ES.SDGC.BU.28061.building.gml' 
  using driver `GML'
Simple feature collection with 7974 features and 24 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 411870.3 ymin: 4485115 xmax: 420339.1 ymax: 4498197
Projected CRS: ETRS89 / UTM zone 30N
#nombres de las columnas de datos
names(edificios)
 [1] "gml_id"                                 
 [2] "beginLifespanVersion"                   
 [3] "conditionOfConstruction"                
 [4] "beginning"                              
 [5] "end"                                    
 [6] "endLifespanVersion"                     
 [7] "informationSystem"                      
 [8] "reference"                              
 [9] "localId"                                
[10] "namespace"                              
[11] "horizontalGeometryEstimatedAccuracy"    
[12] "horizontalGeometryEstimatedAccuracy_uom"
[13] "horizontalGeometryReference"            
[14] "referenceGeometry"                      
[15] "currentUse"                             
[16] "numberOfBuildingUnits"                  
[17] "numberOfDwellings"                      
[18] "numberOfFloorsAboveGround"              
[19] "documentLink"                           
[20] "format"                                 
[21] "sourceStatus"                           
[22] "officialAreaReference"                  
[23] "value"                                  
[24] "value_uom"                              
[25] "geometry"                               

Ya tenemos los datos en R como capa espacial, ahora tenemos que arreglar los datos y extraer información.

La columan beginning contiene la fecha de construcción del edificio, pero en formato texto (chr). Debemos leer estos datos y transformarlos en formato Date con ymd_hms() de lubridate y transformar a Date() ya que las horas y minutos poco importan.

Los registros con fecha desconocida, viene como “–01-01T00:00:00” lo que no corresponde a ninguna fecha reconocible. Por eso, reemplazamos el primer “-” por “0000”.

save(edificios, file = "edifGalapagar.RData")
load("edifGalapagar.RData")
# vemos los primeros 15 años de construccion
head(edificios$beginning, 15)
 [1] "1975-01-01T00:00:00" "1979-01-01T00:00:00" "1970-01-01T00:00:00"
 [4] "1980-01-01T00:00:00" "1986-01-01T00:00:00" "1995-01-01T00:00:00"
 [7] "1975-01-01T00:00:00" "1950-01-01T00:00:00" "1975-01-01T00:00:00"
[10] "1930-01-01T00:00:00" "1960-01-01T00:00:00" "1940-01-01T00:00:00"
[13] "1970-01-01T00:00:00" "1960-01-01T00:00:00" "1950-01-01T00:00:00"
library(stringr) # manejo de palabras y textos
library(lubridate)

# Cambiar nombre y convertir a fecha 
edificios <- mutate(edificios, 
               beginning = str_replace(beginning, "^-", "0000") %>% 
                            ymd_hms() %>% as_date())
edificios
Simple feature collection with 7974 features and 24 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 411870.3 ymin: 4485115 xmax: 420339.1 ymax: 4498197
Projected CRS: ETRS89 / UTM zone 30N
First 10 features:
                      gml_id beginLifespanVersion conditionOfConstruction
1  ES.SDGC.BU.000100100VK19D  2016-06-06T00:00:00              functional
2  ES.SDGC.BU.000100200VK19D  2011-05-11T00:00:00              functional
3  ES.SDGC.BU.000100300VK19D  2011-05-11T00:00:00              functional
4  ES.SDGC.BU.000100500VK19D  2011-05-11T00:00:00              functional
5  ES.SDGC.BU.000100600VK19D  2011-05-27T00:00:00              functional
6  ES.SDGC.BU.000100800VK19D  2011-05-11T00:00:00              functional
7  ES.SDGC.BU.000200100VK19B  2011-05-19T00:00:00              functional
8  ES.SDGC.BU.000200100VK19D  2016-06-06T00:00:00              functional
9  ES.SDGC.BU.000300100VK19B  2016-06-06T00:00:00              functional
10 ES.SDGC.BU.000300200VK19B  2016-02-26T00:00:00              functional
    beginning                 end endLifespanVersion
1  1975-01-01 2011-01-01T00:00:00               <NA>
2  1979-01-01 1979-01-01T00:00:00               <NA>
3  1970-01-01 1970-01-01T00:00:00               <NA>
4  1980-01-01 1980-01-01T00:00:00               <NA>
5  1986-01-01 1986-01-01T00:00:00               <NA>
6  1995-01-01 1995-01-01T00:00:00               <NA>
7  1975-01-01 1975-01-01T00:00:00               <NA>
8  1950-01-01 2011-01-01T00:00:00               <NA>
9  1975-01-01 1975-01-01T00:00:00               <NA>
10 1930-01-01 1999-01-01T00:00:00               <NA>
                                                                              informationSystem
1  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0001001&rc2=00VK19D
2  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0001002&rc2=00VK19D
3  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0001003&rc2=00VK19D
4  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0001005&rc2=00VK19D
5  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0001006&rc2=00VK19D
6  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0001008&rc2=00VK19D
7  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0002001&rc2=00VK19B
8  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0002001&rc2=00VK19D
9  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0003001&rc2=00VK19B
10 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0003002&rc2=00VK19B
        reference        localId  namespace horizontalGeometryEstimatedAccuracy
1  000100100VK19D 000100100VK19D ES.SDGC.BU                                 0.1
2  000100200VK19D 000100200VK19D ES.SDGC.BU                                 0.1
3  000100300VK19D 000100300VK19D ES.SDGC.BU                                 0.1
4  000100500VK19D 000100500VK19D ES.SDGC.BU                                 0.1
5  000100600VK19D 000100600VK19D ES.SDGC.BU                                 0.1
6  000100800VK19D 000100800VK19D ES.SDGC.BU                                 0.1
7  000200100VK19B 000200100VK19B ES.SDGC.BU                                 0.1
8  000200100VK19D 000200100VK19D ES.SDGC.BU                                 0.1
9  000300100VK19B 000300100VK19B ES.SDGC.BU                                 0.1
10 000300200VK19B 000300200VK19B ES.SDGC.BU                                 0.1
   horizontalGeometryEstimatedAccuracy_uom horizontalGeometryReference
1                                        m                   footPrint
2                                        m                   footPrint
3                                        m                   footPrint
4                                        m                   footPrint
5                                        m                   footPrint
6                                        m                   footPrint
7                                        m                   footPrint
8                                        m                   footPrint
9                                        m                   footPrint
10                                       m                   footPrint
   referenceGeometry    currentUse numberOfBuildingUnits numberOfDwellings
1               TRUE 1_residential                     1                 1
2               TRUE 1_residential                     1                 1
3               TRUE 1_residential                     1                 1
4               TRUE 1_residential                     1                 1
5               TRUE 1_residential                     1                 1
6               TRUE 2_agriculture                     1                 0
7               TRUE  3_industrial                     1                 0
8               TRUE 2_agriculture                     1                 0
9               TRUE 1_residential                     1                 1
10              TRUE 1_residential                     1                 1
   numberOfFloorsAboveGround
1                       <NA>
2                       <NA>
3                       <NA>
4                       <NA>
5                       <NA>
6                       <NA>
7                       <NA>
8                       <NA>
9                       <NA>
10                      <NA>
                                                                                                                       documentLink
1  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100100VK19D
2  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100200VK19D
3  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100300VK19D
4  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100500VK19D
5  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100600VK19D
6  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100800VK19D
7  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000200100VK19B
8  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000200100VK19D
9  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000300100VK19B
10 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000300200VK19B
   format sourceStatus officialAreaReference value value_uom
1    jpeg  NotOfficial        grossFloorArea  2371        m2
2    jpeg  NotOfficial        grossFloorArea   644        m2
3    jpeg  NotOfficial        grossFloorArea   130        m2
4    jpeg  NotOfficial        grossFloorArea   388        m2
5    jpeg  NotOfficial        grossFloorArea  1018        m2
6    jpeg  NotOfficial        grossFloorArea   698        m2
7    jpeg  NotOfficial        grossFloorArea     8        m2
8    jpeg  NotOfficial        grossFloorArea   440        m2
9    jpeg  NotOfficial        grossFloorArea   494        m2
10   jpeg  NotOfficial        grossFloorArea   118        m2
                         geometry
1  MULTIPOLYGON (((415230.6 44...
2  MULTIPOLYGON (((415481.2 44...
3  MULTIPOLYGON (((415529.7 44...
4  MULTIPOLYGON (((415509.6 44...
5  MULTIPOLYGON (((415479.3 44...
6  MULTIPOLYGON (((414962.1 44...
7  MULTIPOLYGON (((415918.9 44...
8  MULTIPOLYGON (((416834.8 44...
9  MULTIPOLYGON (((416967.8 44...
10 MULTIPOLYGON (((417176.7 44...
#podemos sacar todas las fotos

head(edificios$documentLink,5)
[1] "http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100100VK19D"
[2] "http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100200VK19D"
[3] "http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100300VK19D"
[4] "http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100500VK19D"
[5] "http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000100600VK19D"
# fechas de construccion limpias
head(edificios$beginning, 15)
 [1] "1975-01-01" "1979-01-01" "1970-01-01" "1980-01-01" "1986-01-01"
 [6] "1995-01-01" "1975-01-01" "1950-01-01" "1975-01-01" "1930-01-01"
[11] "1960-01-01" "1940-01-01" "1970-01-01" "1960-01-01" "1950-01-01"

Antes de hacer el mapa, hacemos una gráfica en la que veremos los periodos de expansión y contracción de la edificación municipal. Usaremos el paquete ggplot2 con la geometría de geom_density() para este objetivo. Añadimos la plantilla cowplot y simplificado algunas opciones.

library(cowplot)
library(tidyverse)

#limitamos al periodo posterior a 1799
filter(edificios, beginning >= "1799-01-01") %>%
 ggplot(aes(beginning)) + 
   # geom_histogram(aes(y=..density..), colour="black", fill="white", alpha=0.5,position="identity") +
    geom_density(fill = "#2166ac", alpha = 0.7) +
    # quito la notación científica que no me gusta aquí
    scale_y_continuous(labels=function(n){format(n, scientific = FALSE)})+
    scale_x_date(date_breaks = "10 year", 
               date_labels = "%Y") + # muestra solo el año en la etiqueta
    theme_cowplot(9)+ # aplico la plantilla cowplot 9
  # finalmente añado algunas lineas para marcar picos de construcción
    labs(y = "",x = "", title = "Evolución del desarrollo urbano en Galapagar")+
        geom_vline(aes(xintercept=as.Date("1900-01-01")),linetype="dashed")+
        # geom_vline(aes(xintercept=as.Date("1930-01-01")),linetype="dashed")+
        geom_vline(aes(xintercept=as.Date("1992-01-01")),linetype="dashed") +
        geom_vline(aes(xintercept=as.Date("1979-01-01")),linetype="dashed")

        # geom_vline(aes(xintercept=as.Date("2001-01-01")),linetype="dashed")
edificiosdf <- filter(edificios, beginning >= "1799-01-01") # filtramos construcciones mayor de 1979
edificiosdf <- mutate(edificiosdf, year = year(beginning)) # creamos una columna con year
edificiosdf <- as.data.frame(edificiosdf) # tranformamos dataframe
# edificiosdf

polo <- count(edificiosdf, as.numeric(year))
polo
   as.numeric(year)   n
1              1900  39
2              1911   1
3              1920   8
4              1930  34
5              1931   1
6              1935   5
7              1940 104
8              1942   2
9              1944   4
10             1945  13
11             1947   1
12             1948   2
13             1949   1
14             1950 170
15             1951   1
16             1952   3
17             1954   1
18             1955  26
19             1956   3
20             1957   5
21             1958   5
22             1959   2
23             1960 303
24             1961   3
25             1962  10
26             1963   6
27             1964   1
28             1965 175
29             1966  12
30             1967  18
31             1968  27
32             1969  23
33             1970 338
34             1971  15
35             1972  87
36             1973  39
37             1974  27
38             1975 341
39             1976  25
40             1977 157
41             1978 200
42             1979  95
43             1980 441
44             1981  37
45             1982  84
46             1983  52
47             1984  83
48             1985 259
49             1986 105
50             1987 165
51             1988 132
52             1989 217
53             1990 468
54             1991 219
55             1992 460
56             1993 223
57             1994 133
58             1995 320
59             1996 177
60             1997 192
61             1998 207
62             1999 137
63             2000 191
64             2001 201
65             2002 116
66             2003 151
67             2004  75
68             2005 127
69             2006 103
70             2007 189
71             2008  83
72             2009  36
73             2010  27
74             2011  26
75             2012  13
76             2013  12
77             2014  20
78             2015   8
79             2016  14
80             2017  10
81             2018  21
82             2019  37
83             2020  37
84             2021  27
# write.xlsx(polo, "poloxls_1.xlsx")

h <- hist(edificiosdf$year, main = "Histograma construcciones por años en Galapagar", 
     xlab = "Año", ylab = "nº de constrcciones", col = "azure", ylim=c(0,3000))

text(h$mids, h$counts, labels = h$counts, adj=c(0.5, -0.5))

ggplot(data.frame(edificiosdf), aes(x = year)) + 
  geom_histogram(color = "gray", fill = "pink")+
  labs(title = "Histograma construcciones por años en Galapagar",
       x = "Años",
       y = "nº de construcciones",
       caption = "informacion de catastro")

# obtenemos las coordinadas de Galapagar
ciudad_point <- tmaptools::geocode_OSM("Galapagar, Madrid", as.sf = TRUE)

# proyectamos los datos
ciudad_point <- st_transform(ciudad_point, 25830) # crs de catastro

# creamos un buffer
point_bf <- st_buffer(ciudad_point, 2000)

# projected CRS:  ETRS89 / UTM zone 30N

# obtenemos la intersección entre el buffer y la edificación
# originalmente eran 2,5 km de ahí el nombre
radio25 <- st_intersection(edificios, point_bf)

#añadimos un nuevo campo o columna con el año de la edificación
radio25 <- mutate(radio25, year = year(beginning))
radio25
Simple feature collection with 3472 features and 32 fields
Active geometry column: geometry
Geometry type: GEOMETRY
Dimension:     XY
Bounding box:  xmin: 413133.3 ymin: 4490588 xmax: 416985.9 ymax: 4494282
Projected CRS: ETRS89 / UTM zone 30N
First 10 features:
                      gml_id beginLifespanVersion conditionOfConstruction
7  ES.SDGC.BU.000200100VK19B  2011-05-19T00:00:00              functional
9  ES.SDGC.BU.000300100VK19B  2016-06-06T00:00:00              functional
22 ES.SDGC.BU.000500100VK19A  2011-05-06T00:00:00              functional
25 ES.SDGC.BU.000600200VK19D  2016-06-06T00:00:00              functional
36 ES.SDGC.BU.001100100VK19B  2011-05-19T00:00:00              functional
53 ES.SDGC.BU.001700100VK19D  2016-06-06T00:00:00              functional
65 ES.SDGC.BU.002000100VK19C  2016-06-06T00:00:00              functional
67 ES.SDGC.BU.002000200VK19C  2016-06-06T00:00:00              functional
69 ES.SDGC.BU.002000300VK19C  2011-05-06T00:00:00              functional
71 ES.SDGC.BU.002100100VK19D  2016-06-06T00:00:00              functional
    beginning                 end endLifespanVersion
7  1975-01-01 1975-01-01T00:00:00               <NA>
9  1975-01-01 1975-01-01T00:00:00               <NA>
22 1970-01-01 1970-01-01T00:00:00               <NA>
25 1975-01-01 2006-01-01T00:00:00               <NA>
36 1980-01-01 1980-01-01T00:00:00               <NA>
53 1992-01-01 2007-01-01T00:00:00               <NA>
65 2004-01-01 2014-01-01T00:00:00               <NA>
67 1995-01-01 1999-01-01T00:00:00               <NA>
69 1994-01-01 1994-01-01T00:00:00               <NA>
71 1960-01-01 1999-01-01T00:00:00               <NA>
                                                                              informationSystem
7  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0002001&rc2=00VK19B
9  https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0003001&rc2=00VK19B
22 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0005001&rc2=00VK19A
25 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0006002&rc2=00VK19D
36 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0011001&rc2=00VK19B
53 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0017001&rc2=00VK19D
65 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0020001&rc2=00VK19C
67 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0020002&rc2=00VK19C
69 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0020003&rc2=00VK19C
71 https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?rc1=0021001&rc2=00VK19D
        reference        localId  namespace horizontalGeometryEstimatedAccuracy
7  000200100VK19B 000200100VK19B ES.SDGC.BU                                 0.1
9  000300100VK19B 000300100VK19B ES.SDGC.BU                                 0.1
22 000500100VK19A 000500100VK19A ES.SDGC.BU                                 0.1
25 000600200VK19D 000600200VK19D ES.SDGC.BU                                 0.1
36 001100100VK19B 001100100VK19B ES.SDGC.BU                                 0.1
53 001700100VK19D 001700100VK19D ES.SDGC.BU                                 0.1
65 002000100VK19C 002000100VK19C ES.SDGC.BU                                 0.1
67 002000200VK19C 002000200VK19C ES.SDGC.BU                                 0.1
69 002000300VK19C 002000300VK19C ES.SDGC.BU                                 0.1
71 002100100VK19D 002100100VK19D ES.SDGC.BU                                 0.1
   horizontalGeometryEstimatedAccuracy_uom horizontalGeometryReference
7                                        m                   footPrint
9                                        m                   footPrint
22                                       m                   footPrint
25                                       m                   footPrint
36                                       m                   footPrint
53                                       m                   footPrint
65                                       m                   footPrint
67                                       m                   footPrint
69                                       m                   footPrint
71                                       m                   footPrint
   referenceGeometry         currentUse numberOfBuildingUnits numberOfDwellings
7               TRUE       3_industrial                     1                 0
9               TRUE      1_residential                     1                 1
22              TRUE      2_agriculture                     1                 0
25              TRUE      2_agriculture                     1                 0
36              TRUE      1_residential                     1                 1
53              TRUE      1_residential                     1                 1
65              TRUE       3_industrial                     1                 0
67              TRUE 4_3_publicServices                     1                 0
69              TRUE         4_2_retail                     1                 0
71              TRUE      2_agriculture                     1                 0
   numberOfFloorsAboveGround
7                       <NA>
9                       <NA>
22                      <NA>
25                      <NA>
36                      <NA>
53                      <NA>
65                      <NA>
67                      <NA>
69                      <NA>
71                      <NA>
                                                                                                                       documentLink
7  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000200100VK19B
9  http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000300100VK19B
22 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000500100VK19A
25 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=000600200VK19D
36 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=001100100VK19B
53 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=001700100VK19D
65 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=002000100VK19C
67 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=002000200VK19C
69 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=002000300VK19C
71 http://ovc.catastro.meh.es/OVCServWeb/OVCWcfLibres/OVCFotoFachada.svc/RecuperarFotoFachadaGet?ReferenciaCatastral=002100100VK19D
   format sourceStatus officialAreaReference value value_uom             query
7    jpeg  NotOfficial        grossFloorArea     8        m2 Galapagar, Madrid
9    jpeg  NotOfficial        grossFloorArea   494        m2 Galapagar, Madrid
22   jpeg  NotOfficial        grossFloorArea  5423        m2 Galapagar, Madrid
25   jpeg  NotOfficial        grossFloorArea   426        m2 Galapagar, Madrid
36   jpeg  NotOfficial        grossFloorArea   538        m2 Galapagar, Madrid
53   jpeg  NotOfficial        grossFloorArea   502        m2 Galapagar, Madrid
65   jpeg  NotOfficial        grossFloorArea  1870        m2 Galapagar, Madrid
67   jpeg  NotOfficial        grossFloorArea  9940        m2 Galapagar, Madrid
69   jpeg  NotOfficial        grossFloorArea  1750        m2 Galapagar, Madrid
71   jpeg  NotOfficial        grossFloorArea   295        m2 Galapagar, Madrid
       lat       lon  lat_min lat_max   lon_min   lon_max
7  40.5775 -4.003775 40.50766  40.633 -4.044367 -3.937901
9  40.5775 -4.003775 40.50766  40.633 -4.044367 -3.937901
22 40.5775 -4.003775 40.50766  40.633 -4.044367 -3.937901
25 40.5775 -4.003775 40.50766  40.633 -4.044367 -3.937901
36 40.5775 -4.003775 40.50766  40.633 -4.044367 -3.937901
53 40.5775 -4.003775 40.50766  40.633 -4.044367 -3.937901
65 40.5775 -4.003775 40.50766  40.633 -4.044367 -3.937901
67 40.5775 -4.003775 40.50766  40.633 -4.044367 -3.937901
69 40.5775 -4.003775 40.50766  40.633 -4.044367 -3.937901
71 40.5775 -4.003775 40.50766  40.633 -4.044367 -3.937901
                             bbox                       geometry year
7  POLYGON ((-4.044367 40.5076... POLYGON ((415916 4492239, 4... 1975
9  POLYGON ((-4.044367 40.5076... MULTIPOLYGON (((416971.2 44... 1975
22 POLYGON ((-4.044367 40.5076... MULTIPOLYGON (((414059.1 44... 1970
25 POLYGON ((-4.044367 40.5076... MULTIPOLYGON (((414986 4494... 1975
36 POLYGON ((-4.044367 40.5076... POLYGON ((415764.2 4491060,... 1980
53 POLYGON ((-4.044367 40.5076... POLYGON ((416158.4 4493283,... 1992
65 POLYGON ((-4.044367 40.5076... MULTIPOLYGON (((414511.6 44... 2004
67 POLYGON ((-4.044367 40.5076... MULTIPOLYGON (((414306.8 44... 1995
69 POLYGON ((-4.044367 40.5076... POLYGON ((414541.6 4493054,... 1994
71 POLYGON ((-4.044367 40.5076... MULTIPOLYGON (((415809.9 44... 1960
library(sf)

library(leaflet)

# map_galapagar1<-st_transform(radio25,crs=4326)

# save(map_galapagar1, file = "map_galapagar1.RData") 
load("map_galapagar1.RData")

# creo escala de 6 colores con 
col_spec <- RColorBrewer::brewer.pal(6, "Spectral")

pal <- colorNumeric(
  palette = col_spec,
  domain = map_galapagar1$year)

# como no hay apenas información anterior a 1900  
map_galapagar1$year[map_galapagar1$year<1900]<-1900

# añado una columna para popup del mapa
map_galapagar1 %>% mutate(popup_info = paste(sep = "<br/>", paste0("<b>","<i>", year,"</i>", "</b>"), currentUse)) %>% 
mutate(lon=ifelse(is.na(lon), address.lon, lon),
       lat=ifelse(is.na(lat), address.lat, lat)) %>%
filter(!is.na(lon) & !grepl("CLOSED", year)) -> map_galapagar1


# Pintamos el Mapa final en leaflet
leaflet(map_galapagar1) %>% 
  addProviderTiles(providers$Stamen.TonerLite)%>% 
  addPolygons(data = map_galapagar1,
                    stroke = TRUE,
                    fill = TRUE,
                    color = ~pal(year),
                    fillOpacity = 0.75,
                    popup = ~popup_info) %>%
  addLegend("bottomright", pal = pal, values = ~year,
                    title = "Antiguedad edificacion",
                    opacity = 1)